/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Alexandre Croiseaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.display.gui.components
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import marcel.display.gui.form.IFormComponent;
	
	/**
	* Dispatched when the value has changed
	* @eventType flash.events.Event.CHANGE
	*/
	[Event(name = "change", type = "flash.events.Event")]

	/**
	 * A RadioGroup component class to use in forms for grouping checkboxes.
	 * @author Alexandre Croiseaux
	 */
	public class RadioGroup extends EventDispatcher implements IFormComponent
	{
		//--------------------------------------------------------------------------
		//
		//  Private vars
		//
		//--------------------------------------------------------------------------
		private var _cbs:/*CheckBox*/Array;
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Constructs a new RadioGroup instance
		 */
		public function RadioGroup()
		{
			_cbs = [];
		}
		
		/**
		 * Registers a checkbox inctance to add in the current group
		 * @param	cb	A Checkbox instance
		 */
		public function addCheckBox(cb:CheckBox):void
		{
			cb.addEventListener(Event.CHANGE, onClickCB);
			_cbs.push(cb);
		}
		
		/**
		 * Returns the first CheckBox of the list, used to display error badge
		 * @return	A Checkbox instance
		 */
		public function getFirstCB():CheckBox
		{
			return _cbs[0];
		}
		
		/**
		 * Returns the name of the selected combobox, or false if no combobox is selected
		 * @return
		 */
		public function getValue():*
		{
			for (var i:int = 0; i < _cbs.length; i++)
			{
				if (_cbs[i].selected) return _cbs[i].name;
			}
			return false;
		}
		
		/**
		 * Selects the specified checkbox by name.
		 * @param	oValue	the name of the inner checkbox to select.
		 */
		public function setValue(selectedName:*) : void
		{
			for (var i:int = 0; i < _cbs.length; i++)
			{
				_cbs[i].selected = selectedName == _cbs[i].name;
			}
		}
		
		/**
		 * Displays the current component as having an error or not. This method is automatically called by the Form class if form.useHighlight is set to true.
		 * @param	hasError	Indicates if there is an error or not.
		 */
		public function displayError(hasError:Boolean):void
		{
			for (var i:int = 0; i < _cbs.length; i++)
			{
				var item:CheckBox = _cbs[i];
				item.displayError(hasError);
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Events
		//
		//--------------------------------------------------------------------------
		private function onClickCB(e:Event):void
		{
			var cb:CheckBox = e.target as CheckBox;
			if (!cb.selected) cb.selected = true;
			else
			{
				for (var i:int = 0; i < _cbs.length; i++)
				{
					_cbs[i].selected = _cbs[i] == cb;
				}
			}
			
			dispatchEvent(new Event(Event.CHANGE));
		}
	}

}